package com.cs307.redcross.client;

import java.io.Serializable;
import java.util.Vector;

import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.user.client.ui.Label;

public class AddPersonPopupPanel extends DialogBox implements Serializable {
	private static final long serialVersionUID = -4534126798396362768L;
	Vector<Person> personVector = new Vector<Person>();
	Vector<Person> addedPersonVector = new Vector<Person>();
	
	Person selectedViewPerson;
	Person selectedAddPerson;
	
	ListBox viewPersonListbox;
	ListBox addedPersonListbox;
	Button addButton;
	Button removeButton;
	Button addPersonButton;
	Button cancelButton;
	
	/**
	 * @wbp.parser.constructor
	 */
	public AddPersonPopupPanel(Vector<Person> _personVector) {
		this.setText("Add Volunteers");
		this.setGlassEnabled(true);	
		setSize("604px", "361px");
		
		AbsolutePanel absolutePanel = new AbsolutePanel();
		setWidget(absolutePanel);
		absolutePanel.setSize("592px", "419px");
		
		viewPersonListbox = new ListBox();
		viewPersonListbox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				if (viewPersonListbox.getSelectedIndex() != -1) {
					selectedViewPerson = personVector.get(viewPersonListbox.getSelectedIndex());
				}
			}
		});
		absolutePanel.add(viewPersonListbox, 10, 34);
		viewPersonListbox.setSize("215px", "282px");
		viewPersonListbox.setVisibleItemCount(15);
		
		personVector = new Vector<Person>();
		if (_personVector != null && _personVector.capacity() > 0) {
			for (Person person : _personVector) {
				if (person == null)
					continue;
				
				viewPersonListbox.addItem(person.getFirstName() + " " + person.getLastName());
				personVector.add(person);
			}
		}		
		
		addedPersonListbox = new ListBox();
		addedPersonListbox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				if (addedPersonListbox.getSelectedIndex() != -1) {
					selectedAddPerson = addedPersonVector.get(addedPersonListbox.getSelectedIndex());
				}
			}
		});
		addedPersonListbox.setVisibleItemCount(15);
		absolutePanel.add(addedPersonListbox, 367, 34);
		addedPersonListbox.setSize("215px", "282px");
		
		addButton = new Button("Add >");
		addButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (selectedViewPerson != null && viewPersonListbox.getSelectedIndex() != -1) {
					int selectedIndex = viewPersonListbox.getSelectedIndex();
					addedPersonListbox.addItem(selectedViewPerson.getFirstName() + " " + selectedViewPerson.getLastName());
					addedPersonVector.add(selectedViewPerson);
					viewPersonListbox.removeItem(viewPersonListbox.getSelectedIndex());
					personVector.remove(selectedViewPerson);
					viewPersonListbox.setSelectedIndex(selectedIndex);
					
					if (viewPersonListbox.getItemCount() > 0) {
						viewPersonListbox.setItemSelected(selectedIndex, true);
					}
					
					if (viewPersonListbox.getSelectedIndex() != -1) {
						selectedViewPerson = personVector.get(viewPersonListbox.getSelectedIndex());
					}
					else {
						selectedViewPerson = null;
					}
				}
			}
		});
		absolutePanel.add(addButton, 10, 322);
		
		removeButton = new Button("< Remove");
		removeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (selectedAddPerson != null && addedPersonListbox.getSelectedIndex() != -1) {
					int selectedIndex = addedPersonListbox.getSelectedIndex();
					viewPersonListbox.addItem(selectedAddPerson.getFirstName() + " " + selectedAddPerson.getLastName());
					personVector.add(selectedAddPerson);
					addedPersonListbox.removeItem(addedPersonListbox.getSelectedIndex());
					addedPersonVector.remove(selectedAddPerson);
					addedPersonListbox.setSelectedIndex(selectedIndex);
					addedPersonListbox.setItemSelected(selectedIndex, true);
					if (addedPersonListbox.getSelectedIndex() != -1) {
						selectedAddPerson = addedPersonVector.get(addedPersonListbox.getSelectedIndex());
					}
					else {
						selectedAddPerson = null;
					}
				}
			}
		});
		absolutePanel.add(removeButton, 367, 322);
		removeButton.setSize("86px", "24px");
		
		addPersonButton = new Button("Add Volunteers");
		absolutePanel.add(addPersonButton, 268, 381);
		addPersonButton.setSize("154px", "28px");
		
		cancelButton = new Button("Cancel");
		absolutePanel.add(cancelButton, 428, 381);
		cancelButton.setSize("154px", "28px");
		
		Label lblAvailableVolunteers = new Label("Available Volunteers:");
		absolutePanel.add(lblAvailableVolunteers, 10, 10);
		
		Label lblAddedVolunteers = new Label("Added Volunteers:");
		absolutePanel.add(lblAddedVolunteers, 367, 10);
		lblAddedVolunteers.setSize("123px", "18px");
	}

	public AddPersonPopupPanel() {		
	}
}
